package com.fastgwt.server.services;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import com.fastgwt.server.dao.EmployeeDAO;
import com.fastgwt.shared.dto.Employee;
import com.fastgwt.shared.services.EmployeeService;

@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {

	/**
	 *
	 */
	private static final long serialVersionUID = -5908521495620045770L;

	



	@Autowired
	private EmployeeDAO employeeDAO;

	@PostConstruct
	public void init() throws Exception {
	}

	@PreDestroy
	public void destroy() {
	}

	public Employee findEmployee(long employeeId) {

		return employeeDAO.findById(employeeId);

	}

	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void saveEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception {

		Employee Employee = employeeDAO.findById(employeeId);

		if(Employee == null) {
			Employee = new Employee(employeeId, name,surname, jobDescription);
			employeeDAO.persist(Employee);
		}

	}

	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void updateEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception {

		Employee employeeDTO = employeeDAO.findById(employeeId);

		if(employeeDTO != null) {
			employeeDTO.setEmployeeName(name);
			employeeDTO.setEmployeeSurname(surname);
			employeeDTO.setJob(jobDescription);
		}

	}

	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void deleteEmployee(long employeeId) throws Exception {

		Employee employeeDTO = employeeDAO.findById(employeeId);

		if(employeeDTO != null)
			employeeDAO.remove(employeeDTO);

	}

	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void saveOrUpdateEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception {

		Employee employeeDTO = new Employee(employeeId, name,surname, jobDescription);

		employeeDAO.merge(employeeDTO);

	}
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public Employee saveOrUpdateEmployee(Employee employee) throws Exception {
		// TODO Auto-generated method stub
		return employeeDAO.merge(employee);
	}

	@Override
	public List<Employee> listAll() throws Exception {
		// TODO Auto-generated method stub
		return employeeDAO.findAll();
	}

	@Override
	public List<Employee> listAll(String name, String surname, String job)
			throws Exception {
		// TODO Auto-generated method stub
		return employeeDAO.listAll(name, surname, job);
	}


}
